package com.yeyuanqi.hottop.scheduletask;

import com.yeyuanqi.hottop.service.*;
import com.yeyuanqi.hottop.vo.HotListVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 每日最火热搜定时任务
 * @author: yeyuanqi
 * @date: 2022/1/14
 */
@EnableAsync
@Component
public class DailyTopScheduleTask {

    private static final Logger logger = LoggerFactory.getLogger(DailyTopScheduleTask.class);

    @Autowired
    RedisService redisService;

    @Autowired
    TBaiduhotService tBaiduhotService;

    @Autowired
    TJinrihotService tJinrihotService;

    @Autowired
    TTxhotService tTxhotService;

    @Autowired
    TWeibohotService tWeibohotService;

    /**
     * 每小时第0分10秒，15分10s，30m10s,45m10s触发一次
     * 将今日top的最新热搜信息进行缓存管理
     */
    //百度每日top缓存更新
    @Scheduled(cron = "20 0,15,30,45 * * * ?")
    public void bdDailyTOP(){
        //获取当前日期，年-月-日格式，作为zset的key
        String now = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        logger.info(now+"百度日top更新");
        String topdaily = now + "-baidudailytop";
        List<HotListVO> hotListVOS = tBaiduhotService.selectNewHot();
        //返回当日top热搜信息
        long count = redisService.zCount(topdaily);
        if (count==0){
            //以防万一，删除该zset的全部元素
            redisService.zDel(topdaily);
            // 没有当日top热搜缓存，将最新top10热搜作为当日top热搜
            for (int i = 0;i<10;i++){
                // 前十条热搜以zset类型缓存
                redisService.zIncr(topdaily,hotListVOS.get(i).getTopic(), Double.valueOf(hotListVOS.get(i).getHot()));
            }
        }else{
            //获取缓存中日top10热度最低
            Double aDouble = redisService.zMin(topdaily);
            //比较热度，进行替换，
            for (HotListVO hotListVO : hotListVOS){
                //热度比最小高，再比较缓存top10中是否已经存在该话题
                if (hotListVO.getHot()>aDouble){
                    Double aDouble1 = redisService.zScore(topdaily, hotListVO.getTopic());
                    //缓存top不存在，则增加该话题热搜
                    if (aDouble1==null){
                        redisService.zAdd(topdaily,hotListVO.getTopic(),Double.valueOf(hotListVO.getHot()));
                    } else if (aDouble1<hotListVO.getHot()){
                        //缓存top10中已存在的话题热度小于当前最新热度，进行替换
                        redisService.zDel(topdaily,hotListVO.getTopic());
                        redisService.zAdd(topdaily,hotListVO.getTopic(),Double.valueOf(hotListVO.getHot()));
                    }
                } else {
                    //hotListVOS热度递减排序，比min还小，则后面相同
                    break;
                }
            }
            //删除top10之后的热搜缓存
            redisService.zDelRange(topdaily);
        }
    }


    //今日头条每日top缓存更新
    @Scheduled(cron = "20 0,15,30,45 * * * ?")
    public void jrDailyTOP(){
        //获取当前日期，年-月-日格式，作为zset的key
        String now = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        logger.info("今日头条日top更新");
        String topdaily = now + "-jinridailytop";
        List<HotListVO> hotListVOS = tJinrihotService.selectNewHot();
        //返回当日top热搜信息
        long count = redisService.zCount(topdaily);
        if (count==0){
            //以防万一，删除该zset的全部元素
            redisService.zDel(topdaily);
            // 没有当日top热搜缓存，将最新top10热搜作为当日top热搜
            for (int i = 0;i<10;i++){
                // 前十条热搜以zset类型缓存
                redisService.zIncr(topdaily,hotListVOS.get(i).getTopic(), Double.valueOf(hotListVOS.get(i).getHot()));
            }
        }else{
            //获取缓存中日top10热度最低
            Double aDouble = redisService.zMin(topdaily);
            //比较热度，进行替换
            for (HotListVO hotListVO : hotListVOS){
                //热度比最小高，再比较缓存top10中是否已经存在该话题
                if (hotListVO.getHot()>aDouble){
                    Double aDouble1 = redisService.zScore(topdaily, hotListVO.getTopic());
                    //缓存top不存在，则增加该话题热搜
                    if (aDouble1==null){
                        redisService.zAdd(topdaily,hotListVO.getTopic(),Double.valueOf(hotListVO.getHot()));
                    } else if (aDouble1<hotListVO.getHot()){
                        //缓存top10中已存在的话题热度小于当前最新热度，进行替换
                        redisService.zDel(topdaily,hotListVO.getTopic());
                        redisService.zAdd(topdaily,hotListVO.getTopic(),Double.valueOf(hotListVO.getHot()));
                    }
                    //删除top10之后的热搜缓存
                    redisService.zDelRange(topdaily);
                } else {
                    //热度递减排序，比min还小，则后面相同
                    break;
                }
            }
        }
    }

    //腾讯新闻每日top缓存更新
    @Scheduled(cron = "25 0,15,30,45 * * * ?")
    public void txDailyTOP(){
        //获取当前日期，年-月-日格式，作为zset的key
        String now = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        logger.info("腾讯新闻日top更新");
        String topdaily = now + "-tengxundailytop";
        List<HotListVO> hotListVOS = tTxhotService.selectNewHot();
        //返回当日top热搜信息
        long count = redisService.zCount(topdaily);
        if (count==0){
            //以防万一，删除该zset的全部元素
            redisService.zDel(topdaily);
            // 没有当日top热搜缓存，将最新top10热搜作为当日top热搜
            for (int i = 0;i<10;i++){
                // 前十条热搜以zset类型缓存
                redisService.zIncr(topdaily,hotListVOS.get(i).getTopic(), Double.valueOf(hotListVOS.get(i).getHot()));
            }
        }else{
            //获取缓存中日top10热度最低
            Double aDouble = redisService.zMin(topdaily);
            //比较热度，进行替换
            for (HotListVO hotListVO : hotListVOS){
                //热度比最小高，再比较缓存top10中是否已经存在该话题
                if (hotListVO.getHot()>aDouble){
                    Double aDouble1 = redisService.zScore(topdaily, hotListVO.getTopic());
                    //缓存top不存在，则增加该话题热搜
                    if (aDouble1==null){
                        redisService.zAdd(topdaily,hotListVO.getTopic(),Double.valueOf(hotListVO.getHot()));
                    } else if (aDouble1<hotListVO.getHot()){
                        //缓存top10中已存在的话题热度小于当前最新热度，进行替换
                        redisService.zDel(topdaily,hotListVO.getTopic());
                        redisService.zAdd(topdaily,hotListVO.getTopic(),Double.valueOf(hotListVO.getHot()));
                    }
                    //删除top10之后的热搜缓存
                    redisService.zDelRange(topdaily);
                } else {
                    //热度递减排序，比min还小，则后面相同
                    break;
                }
            }
        }
    }


    //微博每日top缓存更新
    @Scheduled(cron = "25 0,15,30,45 * * * ?")
    public void wbDailyTOP(){
        //获取当前日期，年-月-日格式，作为zset的key
        String now = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        logger.info("微博日top更新");
        String topdaily = now + "-weibodailytop";
        List<HotListVO> hotListVOS = tWeibohotService.selectNewHot();
        //返回当日top热搜信息
        long count = redisService.zCount(topdaily);
        if (count==0){
            //以防万一，删除该zset的全部元素
            redisService.zDel(topdaily);
            // 没有当日top热搜缓存，将最新top10热搜作为当日top热搜
            for (int i = 0;i<10;i++){
                // 前十条热搜以zset类型缓存
                redisService.zIncr(topdaily,hotListVOS.get(i).getTopic(), Double.valueOf(hotListVOS.get(i).getHot()));
            }
        }else{
            //获取缓存中日top10热度最低
            Double aDouble = redisService.zMin(topdaily);
            //比较热度，进行替换
            for (HotListVO hotListVO : hotListVOS){
                //热度比最小高，再比较缓存top10中是否已经存在该话题
                if (hotListVO.getHot()>aDouble){
                    Double aDouble1 = redisService.zScore(topdaily, hotListVO.getTopic());
                    //缓存top不存在，则增加该话题热搜
                    if (aDouble1==null){
                        redisService.zAdd(topdaily,hotListVO.getTopic(),Double.valueOf(hotListVO.getHot()));
                    } else if (aDouble1<hotListVO.getHot()){
                        //缓存top10中已存在的话题热度小于当前最新热度，进行替换
                        redisService.zDel(topdaily,hotListVO.getTopic());
                        redisService.zAdd(topdaily,hotListVO.getTopic(),Double.valueOf(hotListVO.getHot()));
                    }
                    //删除top10之后的热搜缓存
                    redisService.zDelRange(topdaily);
                } else {
                    //热度递减排序，比min还小，则后面相同
                    break;
                }
            }
        }
    }
}
